package com.dds.web.controller.app;

import java.util.List;
import java.util.Map;

import com.dds.system.domain.app.AppUser;
import com.dds.system.domain.union.UnionShopBusInfo;
import com.dds.system.service.admin.IAdminUserOperationService;
import com.dds.system.service.app.IAppUserService;
import com.dds.system.service.union.IUnionShopBusInfoService;
import com.dds.system.service.union.IUnionShopCategoryService;
import com.dds.system.service.union.IUnionShopManagerService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.dds.common.annotation.Log;
import com.dds.common.core.controller.BaseController;
import com.dds.common.core.domain.AjaxResult;
import com.dds.common.enums.BusinessType;
import com.dds.system.domain.app.AppMessageDefine;
import com.dds.system.service.app.IAppMessageDefineService;
import com.dds.common.utils.poi.ExcelUtil;
import com.dds.common.core.page.TableDataInfo;

/**
 * APP中的消息定义Controller
 * 
 * @author gaolianjie
 * @date 2021-04-13
 */
@RestController
@RequestMapping("/app/messageDefine")
public class AppMessageDefineController extends BaseController
{
    @Autowired
    private IAppMessageDefineService appMessageDefineService;

    @Autowired
    private IAdminUserOperationService adminUserOperationService;

    @Autowired
    private IUnionShopCategoryService unionShopCategoryService;

    @Autowired
    private IUnionShopBusInfoService unionShopBusInfoService;

    @Autowired
    private IUnionShopManagerService unionShopManagerService;

    @Autowired
    private IAppUserService appUserService;

    /**
     * 查询APP中的消息定义列表
     */
    @PreAuthorize("@ss.hasPermi('app:messageDefine:list')")
    @GetMapping("/list")
    public TableDataInfo list(AppMessageDefine appMessageDefine)
    {
        startPage();
        List<AppMessageDefine> list = appMessageDefineService.selectAppMessageDefineList(appMessageDefine);
        return getDataTable(list);
    }

    /**
     * 导出APP中的消息定义列表
     */
    @PreAuthorize("@ss.hasPermi('app:messageDefine:export')")
    @Log(title = "APP中的消息定义", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AppMessageDefine appMessageDefine)
    {
        List<AppMessageDefine> list = appMessageDefineService.selectAppMessageDefineList(appMessageDefine);
        ExcelUtil<AppMessageDefine> util = new ExcelUtil<AppMessageDefine>(AppMessageDefine.class);
        return util.exportExcel(list, "define");
    }

    /**
     * 获取APP中的消息定义详细信息
     */
    @PreAuthorize("@ss.hasPermi('app:messageDefine:query')")
    @GetMapping(value = "/{messageId}")
    public AjaxResult getInfo(@PathVariable("messageId") Long messageId)
    {
        return AjaxResult.success(appMessageDefineService.selectAppMessageDefineById(messageId));
    }

    /**
     * 新增APP中的消息定义
     */
    @PreAuthorize("@ss.hasPermi('app:messageDefine:add')")
    @Log(title = "APP中的消息定义", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Map<String, Object> params)
    {
        return toAjax(appMessageDefineService.insertAppMessageDefine(params));
    }

    /**
     * 修改APP中的消息定义
     */
    @PreAuthorize("@ss.hasPermi('app:messageDefine:edit')")
    @Log(title = "APP中的消息定义", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppMessageDefine appMessageDefine)
    {
        return toAjax(appMessageDefineService.updateAppMessageDefine(appMessageDefine));
    }

    /**
     * 删除APP中的消息定义
     */
    @PreAuthorize("@ss.hasPermi('app:messageDefine:remove')")
    @Log(title = "APP中的消息定义", businessType = BusinessType.DELETE)
	@DeleteMapping("/{messageIds}")
    public AjaxResult remove(@PathVariable Long[] messageIds)
    {
        return toAjax(appMessageDefineService.deleteAppMessageDefineByIds(messageIds));
    }

    /**
     * 获取运营中心列表
     * @return
     */
    @GetMapping("/getOperationList")
    public AjaxResult getOperationList() {
        return AjaxResult.success(adminUserOperationService.selectAdminUserOperationList(null));
    }

    /**
     * 获取店铺分类
     * @return
     */
    @GetMapping("/getIndustryList")
    public AjaxResult getIndustryList() {
        return AjaxResult.success(unionShopCategoryService.selectUnionShopCategoryList(null));
    }

    /**
     * 获取店铺列表
     * @param shopName
     * @return
     */
    @GetMapping("/getShopList")
    public AjaxResult getShopList(
            @RequestParam(name = "shopName") String shopName
    ) {
        return AjaxResult.success(unionShopBusInfoService.selectShopsByShopName(shopName));
    }

    /**
     * 获取店铺管理员
     * @param shopId
     * @return
     */
    @GetMapping("/getShopManager")
    public AjaxResult getShopManager(
            @RequestParam(name = "shopId") String shopId
    ) {
        return AjaxResult.success(unionShopManagerService.selectManagerByShopId(shopId));
    }

    /**
     * 获取用户列表
     * @param username
     * @return
     */
    @GetMapping("/getUserList")
    public TableDataInfo getUserList(
            @RequestParam(name = "username") String username
    ) {
        AppUser appUser = AppUser.of().setUsername(username);

        startPage();
        List<AppUser> list = appUserService.selectAppUserList(appUser);
        return getDataTable(list);
    }

    @GetMapping("/sendMessage/{messageId}")
    public AjaxResult sendMessage(@PathVariable("messageId") Long messageId) {
        boolean res = appMessageDefineService.sendMessage(messageId);
        if (res) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }
}
